<?php

/**
 * bug设置类
 * author: kingsky
 * time: 2014-4-26
 */

namespace Admin\Controller;

use Think\Controller;

class CaseController extends CommonController {

    public function _initialize() {
        parent::_initialize();
    }

    public function tlist() {
        $NC = M('ProjectType');
        $count = $NC->count();
        $Page = new \Think\Page($count, 10);
        $list = $NC->limit($Page->firstRow . ',' . $Page->listRows)->order('time desc')->select();
        $this->empty = "<td colspan='7' class='tb_heade'>暂无数据</td>";

        $page = $Page->show();

        $this->assign("page", $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function addtype() {
        $NC = M('ProjectType');
        $this->display();
    }

    public function doaddtype() {
        $NC = M('ProjectType');

        if (I('name') != '') {
            $data['name'] = I('post.name');
            $data['remark'] = I('post.remark');
            $data['time'] = time();
            if ($NC->add($data)) {
                $this->success('增加成功');
            } else {
                $this->error('增加失败');
            }
        } else {
            $this->error('项目名称不能为空');
        }
    }

    public function edittype() {
        $id = I('id', '', 'int');
        if ($id == '') {
            $this->error('参数错误');
        }
        $NC = M('ProjectType');
        $this->edit = $NC->where(array('id' => $id))->find();
        $this->display();
    }

    public function doedittype() {
        $NC = M('ProjectType');
        $id = I('id', '', 'int');
        if ($id == '') {
            $this->error('参数错误');
        }
        if (I('name') != '') {
            $data['name'] = I('post.name');
            $data['remark'] = I('post.remark');
            $data['time'] = time();
            $rs = $NC->where(array('id' => $id))->setField($data);
            if ($rs !== false) {
                $this->success('修改成功', U('Case/tlist'));
            } else {
                $this->error('修改失败，稍后再试');
            }
        } else {
            $this->error('项目名称不能为空');
        }
    }

    public function deltype() {
        $id = I('id');
        if ($id != '' && is_numeric($id)) {
            $where['id'] = $id;
            $rs = M('ProjectType')->where($where)->delete();
            if ($rs !== false) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error("参数错误，请稍后再试");
        }
    }

    //buglist
    public function clist() {
        $NC = D('BugView');
        $count = $NC->count();
        $Page = new \Think\Page($count, 10);
        $list = $NC->limit($Page->firstRow . ',' . $Page->listRows)->order('time desc')->select();
        $this->empty = "<td colspan='7' class='tb_heade'>暂无数据</td>";

        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function addcase() {
        $NC = M('ProjectType');
        $ctlist = $NC->field('id,name')->select();

        $this->assign('ctlist', $ctlist);
        $this->display();
    }

    public function doaddcase() {
        if (I('title') == '') {
            $this->error("案例标题不能为空！");
            exit;
        }
        if (I('type') == '0' || I('type') == 0) {
            $this->error("请选择案例分类！");
            exit;
        }
        if ($_POST['content'] == '') {
            $this->error("案例内容不能为空！");
            exit;
        }
        $data = array(
            'title' => I('title'),
            'type' => I('type'),
            'visible' => I('ifshow'),
            'abstract' => I('abstract'),
            'content' => $_POST['content'],
        );
        $upload = new \Think\Upload(); // ;// 实例化上传类
        $upload->maxSize = 512000; // 设置附件上传大小 
        $upload->exts = array('jpg', 'gif', 'jpeg', 'png'); // 设置附件上传类型 
        $upload->autoSub = FALSE; // 设置附件上传类型
        $upload->savePath = './static/'; // 设置附件上传目录 
        $info = $upload->upload();
        if ($info) {
            $image = new \Think\Image();
            $image->open('./Uploads/static/' . $info['image']['savename']);
            // 生成一个居中裁剪为235*235的缩略图
            $image->thumb(235, 235, \Think\Image::IMAGE_THUMB_FILLED)->save('./Uploads/static/thumb_' . $info['image']['savename']);
            $data['image'] = __ROOT__ . '/Uploads/static/thumb_' . $info['image']['savename'];
        } else {
            $data['image'] = '';
        }
        if ($data['image'] == '') {
            $img = bodyimg($data['content']);
            if ($img != '') {
                $data['image'] = $img;
            }
        }

        //没有图片提示错误
        if ($data['image'] == '') {
            $this->error("请上传至少一张图片！");
            exit;
        }

        if ($data['abstract'] == '') {
            $data['abstract'] = utf_substr(strip_tags($data['content']), 480);
        }
        $data['time'] = time();
        $data['content'] = htmlspecialchars($data['content']);
        //dump($data);die;
        $NC = M('Bug');
        if ($rs = $NC->add($data)) {
            $this->success('案例增加成功');
            exit;
        } else {
            $this->success('案例增加失败，稍后重试');
            exit;
        }
    }

    public function scancase() {
        $id = I('id', '', 'int');
        if ($id == '') {
            $this->error('参数错误');
        }
        $NC = M('Bug');
        $case = $NC->where(array('id' => $id))->find();
        $this->assign('case', $case);
        $this->display();
    }

    public function editcase() {
        $id = I('id', '', 'int');
        if ($id == '') {
            $this->error('参数错误');
            exit;
        }
        $edit = M('Bug')->where(array('id' => $id))->find();
        $NC = M('ProjectType');
        $ctlist = $NC->field('id,name')->select();

        $this->assign('ctlist', $ctlist);
        $this->assign('edit', $edit);
        $this->display();
    }

    public function doeditcase() {
        $id = I('id', '', 'int');
        if ($id == '') {
            $this->error('参数错误');
        }

        if (I('title') == '') {
            $this->error("案例标题不能为空！");
            exit;
        }
        if (I('type') == '0' || I('type') == 0) {
            $this->error("请选择案例分类！");
            exit;
        }
        if ($_POST['content'] == '') {
            $this->error("案例内容不能为空！");
            exit;
        }
        $data = array(
            'title' => I('title'),
            'type' => I('type'),
            'visible' => I('ifshow'),
            'abstract' => I('abstract'),
            'content' => $_POST['content'],
        );

        $upload = new \Think\Upload(); // ;// 实例化上传类
        $upload->maxSize = 512000; // 设置附件上传大小 
        $upload->exts = array('jpg', 'gif', 'jpeg', 'png'); // 设置附件上传类型 
        $upload->autoSub = FALSE; // 设置附件上传类型
        $upload->savePath = './static/'; // 设置附件上传目录 
        $info = $upload->upload();
        if ($info) {
            $image = new \Think\Image();
            $image->open('./Uploads/static/' . $info['image']['savename']);
            // 生成一个居中裁剪为235*235的缩略图
            $image->thumb(235, 235, \Think\Image::IMAGE_THUMB_FILLED)->save('./Uploads/static/thumb_' . $info['image']['savename']);
            $data['image'] = __ROOT__ . '/Uploads/static/thumb_' . $info['image']['savename'];
        }

        if ($data['abstract'] == '') {
            $data['abstract'] = utf_substr(strip_tags($data['content']), 480);
        }
        $data['content'] = htmlspecialchars($data['content']);
        //dump($data);die;
        if ($rs = M('Bug')->where(array('id' => $id))->setField($data)) {
            $this->success('异常编辑成功', U('Case/clist'));
            exit;
        } else {
            $this->error('异常编辑失败，稍后重试');
            exit;
        }
    }

    public function delcase() {
        $id = I('id');
        if ($id == '') {
            $this->error('参数错误');
        }
        $rs = M('Bug')->where(array('id' => $id))->delete();
        if ($rs !== false) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}